package org.csu.mypetstore.Controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CategortService;
import org.csu.mypetstore.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategortService categortService;
    @Autowired
    LogService logService;
    @Autowired
    AccountService accountService;
    @Autowired
    CartService cartService;

    @RequestMapping("/ViewCategory")
    public String ViewCategory(HttpServletRequest req,Model model){
        String category;
        category=req.getParameter("categoryId");
        Category category1 = categortService.getCategory(category);
        List<Product> productList= categortService.getProductListByCategory(category);
        HttpSession session = req.getSession();
        Account account = (Account)session.getAttribute("account");
        if(account != null) {
            String logInfo =  " 跳转到商品种类 " + category;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        model.addAttribute("category",category1);
        model.addAttribute("productList",productList);
        return "/catalog/Category";
    }

    @RequestMapping("/ViewItem")
    public String ViewItem(HttpServletRequest req,Model model){
        String itemId;
        HttpSession session = req.getSession();
        Account account = (Account)session.getAttribute("account");
        itemId = req.getParameter("itemId");
        Item item = categortService.getItem(itemId);
        List<Item> itemList = categortService.getItemListByProduct(itemId);
        String instock = categortService.selectinstock(itemId);
        if (account != null){
            String logInfo =" 查看具体商品 " + item;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        model.addAttribute("item",item);
        model.addAttribute("instock",instock);
        model.addAttribute("itemList",itemList);
        Product pruduct = categortService.getProduct(itemId);
        model.addAttribute("product",pruduct);
        return "catalog/Item";
    }

    @GetMapping(value="/ViewProduct")
    public String ViewProduct( Model model, HttpServletRequest req){
        String productId = req.getParameter("productId");
        HttpSession session = req.getSession();
        Account account = (Account)session.getAttribute("account");
        if(account!=null) {
            String logInfo = " 查看产品 " ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        List<Item> itemlist= categortService.getItemListByProduct(productId);
        for(int i=0;i<itemlist.size();i++){
            if(categortService.getQuanatity(itemlist.get(i).getItemId())<=0){
                categortService.updateinstock(itemlist.get(i).getItemId(),"false");
            }
            itemlist.get(i).setInstock(categortService.selectinstock(itemlist.get(i).getItemId()));
        }
        Product product = categortService.getProductByProductID(productId);
        model.addAttribute("product",product);
        model.addAttribute("itemlist",itemlist);
        return ("/catalog/Product");
    }
    @GetMapping(value="/FindResult")
    public void FindResult( HttpServletResponse response, HttpServletRequest req){
        //获取搜索框输入的内容
        String keyword = req.getParameter("keyword");
        //向server层调用相应的业务
        List<Product> productList = categortService.searchProductList(keyword);

        response.setContentType("text/xml");
        try {
            PrintWriter out = response.getWriter();
            //返回结果
            String res = "";
            for(int i=0; i<productList.size(); i++){
                if(i>0){
                    res += "," + productList.get(i).getName();
                }else{
                    res += productList.get(i).getName();
                }
            }
            out.write(res);
            out.flush();
            out.close();
        }catch (Exception e){

    }
}
    @RequestMapping(value="/SearchProducts")
    public String SearchProducts(HttpServletRequest req, Model model){
        String keyword=req.getParameter("keyword");
        //Product pruduct = categortService.getProductByname(keyword);
        List<Product> productList= categortService.searchProductList(keyword);
        HttpSession session = req.getSession();
       // productList.add(pruduct);
       // session.setAttribute("pruduct",pruduct);
        session.setAttribute("productList",productList);
       // model.addAttribute("pruduct",pruduct);
        model.addAttribute("productList",productList);
        Account account = (Account)session.getAttribute("account");

        if(account != null){
            String logInfo = logService.logInfo(" ")  + " 查找商品" ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        return "/catalog/SearchProducts";

    }

}
